--[[-----------------------------------------------------------------------------
CollapsibleGroup Container
Container that can be expanded or collapsed.
-------------------------------------------------------------------------------]]
local Type, Version = "CollapsibleGroup", 1
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end

--[[-----------------------------------------------------------------------------
Scripts
-------------------------------------------------------------------------------]]
local function Button_OnClick(button)
	local self = button.obj
    self:SetCollapsed(not self.isCollapsed)
end

--[[-----------------------------------------------------------------------------
Methods
-------------------------------------------------------------------------------]]
local methods = {
	["OnAcquire"] = function(self)
		self:SetWidth(300)
		self:SetHeight(18)
        self.isCollapsed = false;
        self.button:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-UP")
        self.button:SetPushedTexture("Interface\\Buttons\\UI-MinusButton-DOWN")
        self.content:Show();
	end,

	-- ["OnRelease"] = nil,

	["LayoutFinished"] = function(self, width, height)
		if self.noAutoHeight then return end
        if self.isCollapsed then
            self:SetHeight(18)
        else
            self:SetHeight(height+18)
        end
	end,

	["OnWidthSet"] = function(self, width)
		local content = self.content
		content:SetWidth(width)
		content.width = width
	end,

	["OnHeightSet"] = function(self, height)
		local content = self.content
		content:SetHeight(height)
		content.height = height
	end,
    
    ["SetText"] = function(self, text)
        self.label:SetText(text)
        self.button:SetHitRectInsets(0, -self.label:GetWidth(), 0, 0);
    end,
    
    ["SetCollapsed"] = function(self, collapsed)
        if collapsed then
            self.button:SetNormalTexture("Interface\\Buttons\\UI-PlusButton-UP")
            self.button:SetPushedTexture("Interface\\Buttons\\UI-PlusButton-DOWN")
            self.content:Hide();
        else
            self.button:SetNormalTexture("Interface\\Buttons\\UI-MinusButton-UP")
            self.button:SetPushedTexture("Interface\\Buttons\\UI-MinusButton-DOWN")
            self.content:Show();
        end
        self.isCollapsed = collapsed
        if self.parent and self.parent.PerformLayout then
            self.parent:PerformLayout();
        end
        AceGUI:ClearFocus()
    end,
}

--[[-----------------------------------------------------------------------------
Constructor
-------------------------------------------------------------------------------]]
local function Constructor()

	local frame = CreateFrame("Frame", nil, UIParent)
	frame:SetFrameStrata("FULLSCREEN_DIALOG")
    frame:Show()
    
    local button = CreateFrame("Button", nil, frame)
    button:SetPoint("TOPLEFT", 0, 0)
    button:SetWidth(16)
    button:SetHeight(16)
    button:SetHighlightTexture("Interface\\Buttons\\UI-PlusButton-Hilight")
    button:SetScript("OnClick", Button_OnClick)

	local label = frame:CreateFontString(nil, "BACKGROUND", "GameFontNormal")
	label:SetPoint("LEFT", button, "RIGHT")

	--Container Support
	local content = CreateFrame("Frame", nil, frame)
	content:SetPoint("TOPLEFT", 0, -10)
	content:SetPoint("BOTTOMRIGHT")
    
	local widget = {
		frame        = frame,
        button       = button,
        label        = label,
		content      = content,
		type         = Type
	}
	for method, func in pairs(methods) do
		widget[method] = func
	end
    
    button.obj = widget;
    
	return AceGUI:RegisterAsContainer(widget)
end

AceGUI:RegisterWidgetType(Type, Constructor, Version)
